package com.atguigu.lease.web.interceptors;

import com.atguigu.lease.exception.LeaseException;
import com.atguigu.lease.result.ResultCodeEnum;
import com.atguigu.lease.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author 51560
 * @project lease
 * @title 登录拦截器
 * @package com.atguigu.lease.web.interceptors
 * @since 2025/5/20 下午9:26
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取request中header中的token
        String token = request.getHeader("access_token");
        // 检查token是否为空
        if (StringUtils.isEmpty(token)) {
            throw new LeaseException(ResultCodeEnum.ADMIN_LOGIN_AUTH);
        }
        // 检查token是否过期和有效
        JwtUtil.parseToken(token);
        // true放行 false拦截
        return true;
    }
}
